﻿using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace VerticalMenu.Controls
{
	public partial class SinglePanel : UserControl
	{
		public SinglePanel()
		{
			this.InitializeComponent();
			this.tableListViewScroll.HorizontalContentAlignment = HorizontalAlignment.Stretch;
		}

		public int ID { get; set; }

		public double controlWidth
		{
			set
			{
				this.btnTitle.Width = value;
				this.tableListViewScroll.Width = value;
				base.Width = value;
			}
		}

		public double controlHeight
		{
			set
			{
				this.tableListViewScroll.Height = value;
				base.Height = value + this.btnTitle.Height;
				this.m_PreviousHeight = value + this.btnTitle.Height;
				this.FoldTabControl();
			}
		}

		public TableInfos TabInofs
		{
			set
			{
				this.btnTitle.Content = value.TableName;
				for (int i = 0; i < value.CommandInfoList.Count; i++)
				{
					CommandInfos commandInfos = value.CommandInfoList[i];
					Label label = new Label();
					label.HorizontalAlignment = HorizontalAlignment.Stretch;
					label.Content = commandInfos.CmdName;
					label.Margin = new Thickness(0.0, 5.0, 0.0, 0.0);
					ToolTip toolTip = new ToolTip();
					toolTip.Content = commandInfos.Tooltip;
					StackPanel stackPanel = new StackPanel();
					stackPanel.Orientation = Orientation.Horizontal;
					stackPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
					Image imageByName = CommandInfos.GetImageByName(commandInfos.ImageName);
					stackPanel.Children.Add(imageByName);
					stackPanel.Children.Add(label);
					stackPanel.Background = Brushes.Transparent;
					stackPanel.Cursor = Cursors.Hand;
					stackPanel.Tag = i;
					stackPanel.ToolTip = toolTip;
					stackPanel.MouseDown += this.StackPlane_Click;
					this.tableListViewScroll.Items.Add(stackPanel);
				}
			}
		}

		public event RoutedEventHandler ClickTitle;

		public event RoutedEventHandler ClickItem;

		private void StackPlane_Click(object sender, MouseButtonEventArgs e)
		{
			bool flag = this.ClickItem != null;
			if (flag)
			{
				this.ClickItem(string.Format("{0} . {1}", this.ID, ((StackPanel)sender).Tag), e);
			}
		}

		private void btnTitle_Click(object sender, RoutedEventArgs e)
		{
			bool flag = this.ClickTitle != null;
			if (flag)
			{
				this.ClickTitle(this, e);
			}
		}

		private void lblItem_Click(object sender, RoutedEventArgs e)
		{
			bool flag = this.ClickItem != null;
			if (flag)
			{
				this.ClickItem(string.Format("{0} . {1}", this.ID, ((Label)sender).Tag), e);
			}
		}

		private void InitializeAnimationTimer()
		{
			bool flag = this.m_StartAnimation == null;
			if (flag)
			{
				this.m_StartAnimation = new Timer();
				this.m_StartAnimation.Interval = 10.0;
			}
			this.m_StartAnimation.Elapsed += this.startAnimation_Elapsed;
		}

		private void UninitializeAnimationTimer()
		{
			this.m_StartAnimation.Elapsed -= this.startAnimation_Elapsed;
		}

		private void FoldTabControl()
		{
			bool flag = MainPage.IsInitializedCommandData();
			if (flag)
			{
				DoubleAnimation animation = new DoubleAnimation(base.Height, 23.0, TimeSpan.FromSeconds(0.2));
				base.BeginAnimation(FrameworkElement.HeightProperty, animation);
				this.InitializeAnimationTimer();
				this.m_StartAnimation.Start();
			}
		}

		private void UnfoldTabControl()
		{
			bool flag = MainPage.IsInitializedCommandData();
			if (flag)
			{
				DoubleAnimation animation = new DoubleAnimation(23.0, this.m_PreviousHeight, TimeSpan.FromSeconds(0.2));
				base.BeginAnimation(FrameworkElement.HeightProperty, animation);
			}
		}

		private void startAnimation_Elapsed(object sender, ElapsedEventArgs e)
		{
			bool flag = MainPage.IsInitializedCommandData();
			if (flag)
			{
				base.Dispatcher.Invoke(new DelegateAnimation(this.UnfoldTabControl), null);
				this.m_StartAnimation.Stop();
				this.UninitializeAnimationTimer();
			}
		}

		private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			this.btnTitle.Width = base.ActualWidth;
			this.tableListViewScroll.Width = base.ActualWidth;
		}

		private const double m_InitHeight = 23.0;

		private double m_PreviousHeight = 23.0;

		private Timer m_StartAnimation;
	}
}
